﻿<%@ Control Language="C#" ClassName="TickerControl" %>
<%@ OutputCache Duration="60" VaryByParam="None" %>

<script runat="server">
  protected void Page_Load(object sender, EventArgs e)
  {
    if (!Page.IsPostBack)
    {
      B2C_MALL_HOME ws = new B2C_MALL_HOME();
      ws.Url = ConfigurationManager.AppSettings["B2C_MALL_HOME"].ToString(); 
      string jsonString = 
        ws.B2C_MALL_GetMallTicker(System.Configuration.ConfigurationManager.AppSettings["WebDeviceid"].ToString()
        , HttpContext.Current.User.Identity.Name);

      //以下使用測試資料
      //System.IO.StreamReader sr = new System.IO.StreamReader(Server.MapPath("~/Demo/JSON.txt"));
      //string jsonString = sr.ReadToEnd();

      JObject restoredObject = JsonConvert.DeserializeObject<JObject>(jsonString);
      string Is_Valid = restoredObject["Is_Valid"].ToString();
      string msg = restoredObject["Msg"].ToString();
      if (Is_Valid == "Y")
      {
        dynamic tricker = restoredObject["TickerAry"];
        ListViewTicker.DataSource = tricker;
        ListViewTicker.DataBind();
      }
    }
  }

  protected void ListViewTicker_ItemDataBound(object sender, ListViewItemEventArgs e)
  {
    if (e.Item.ItemType == ListViewItemType.DataItem)
    {
      HyperLink link = e.Item.FindControl("HLTicker") as HyperLink;
      HiddenField target = e.Item.FindControl("HFTarget") as HiddenField;
      if (target.Value == "Y")
      {
        link.Target = "_blank";
      }
      else
      {
        link.Target = "_self";
      }
    }
  }
</script>

<div class="text-slider">
  <div class="next-slide slider"></div>
  <div class="prev-slide slider"></div>
  <div class="text-slider-container">
    <asp:ListView ID="ListViewTicker" runat="server" GroupItemCount="2" OnItemDataBound="ListViewTicker_ItemDataBound">
      <GroupTemplate>
        <tr id="itemPlaceholderContainer" runat="server">
          <td id="itemPlaceholder" runat="server"></td>
        </tr>
      </GroupTemplate>
      <LayoutTemplate>
        <table id="groupPlaceholderContainer" runat="server">
          <tr id="groupPlaceholder" runat="server">
          </tr>
        </table>
      </LayoutTemplate>
      <ItemTemplate>
        <td id="Td1" runat="server">
          <asp:HyperLink ID="HLTicker" runat="server"
            Text='<%# Server.HtmlEncode(Eval("Title").ToString()) %>' 
            NavigateUrl='<%# Server.HtmlEncode(Eval("URL").ToString()) %>' />
          <asp:HiddenField ID="HFTarget" runat="server" Value='<%# Eval("OpenWindow") %>' />
        </td>
      </ItemTemplate>
    </asp:ListView>
  </div>
</div>
